package com.zhaokuner.pm.model.common;

import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * 数据请求类
 *
 * @author zhaokuner
 */
public class BaseReq implements Serializable {

    private static final long serialVersionUID = -5930744574742647638L;
    /**
     * 页码
     */
    public static String PARAMS_PAGE_INDEX = "pageIndex";
    /**
     * 单页条数
     */
    public static String PARAMS_PAGE_SIZE = "pageSize";
    /**
     * 分页标识
     */
    public static String PARAMS_PAGE_MARK = "pageMark";
    /**
     * 数据排序
     */
    public static String PARAMS_ORDER = "order";
    /**
     * 排序字段
     */
    public static String PARAMS_SORT_NAME = "sortName";

    /**
     * 页码
     */
    protected int pageIndex = 1;
    /**
     * 单页条数
     */
    protected int pageSize = 10;
    /**
     * 分页标识
     */
    protected String pageMark;
    /**
     * 开始条数
     */
    protected Integer beginIndex = 0;
    /**
     * 保存当前的页面，用于验证是否需要从新根据页面计算
     **/
    private int pageIndexCurrent = 0;
    /**
     * 数据排序
     */
    protected String order;
    /**
     * 排序字段
     */
    protected String sortName;

    public static final String ORDER_ASC = "asc";
    public static final String ORDER_DESC = "desc";

    public void initPage(BaseReq req) {
        if (null == req) {
            return;
        }
        this.setOrder(req.getOrder());
        this.setPageIndex(req.getPageIndex());
        this.setPageSize(req.getPageSize());
        this.setPageMark(req.getPageMark());
        this.setSortName(req.getSortName());
    }

    public void setOrderAsc() {
        this.setOrder(ORDER_ASC);
    }

    public void setOrderDesc() {
        this.setOrder(ORDER_DESC);
    }

    public boolean notBlankMark() {
        return StringUtils.isNotBlank(pageMark);
    }

    public Integer getBeginIndex() {
        if (Integer.MAX_VALUE == pageSize) {
            return null;
        }
        if (pageIndexCurrent != pageIndex) {
            beginIndex = (this.pageIndex - 1) * pageSize;
            pageIndexCurrent = pageIndex;
        }
        return beginIndex;
    }

    public void setBeginIndex(Integer beginIndex) {
        this.beginIndex = beginIndex;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public String getPageMark() {
        return pageMark;
    }

    public void setPageMark(String pageMark) {
        this.pageMark = pageMark;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public String getSortName() {
        return sortName;
    }

    public void setSortName(String sortName) {
        this.sortName = sortName;
    }

}
